/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.dao;

import br.com.deployment.daogenerico.DaoGenerico;
import br.com.deployment.entidade.acesso.Usuario;
import br.com.deployment.entidade.protocolo.Protocolo;
import br.com.deployment.entidade.tramite.Secao;
import br.com.deployment.entidade.tramite.Tramite;
import br.com.deployment.interfaces.TramiteDaoIn;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class TramiteDao extends DaoGenerico<Tramite> implements TramiteDaoIn {
    
    public TramiteDao() {
        super(Tramite.class);
    }
    
    public List<Tramite> findMyProtocolo(Usuario usuario, Protocolo protocolo) {
        String sql = " Select t from Tramite t JOIN "
                + " u.secao s JOIN "
                + " t.destino d JOIN "
                + " t.protocolo pro, Usuario u"
                + " where s.id = d.id and"
                + " t.status = false and "
                + " u.id = :usr";
        
        if (protocolo != null && protocolo.getId() != 0) {
            sql += " and pro.id = :prot ";
        }
        
        Query query = getEm().createQuery(sql);
        query.setParameter("usr", usuario.getId());
        
        if (protocolo != null && protocolo.getId() != 0) {
            query.setParameter("prot", protocolo.getId());
        }
        
        return query.getResultList();
    }
    
    public Boolean existeTramiteAberto(Protocolo protocolo) {
        String sql = " Select t from Tramite t JOIN "
                + " t.protocolo pro"
                + " where t.status = false "
                + "  and pro.id = :prot ";
        
        Query query = getEm().createQuery(sql);
        query.setParameter("prot", protocolo.getId());
        System.err.println(sql + ""+protocolo.getId());
        
        return !query.getResultList().isEmpty();
    }
    
     public  List<Tramite> allTramiteProtocolo(Protocolo protocolo) {
        String sql = " Select t from Tramite t JOIN "
                + " t.protocolo pro"
                + " where pro.id = :prot ";
        
        Query query = getEm().createQuery(sql);
        query.setParameter("prot", protocolo.getId());
        System.err.println(query.getResultList());
        return query.getResultList();
    }
}
